package com.microblogforsim.tool;

import android.os.AsyncTask;
import android.util.Log;

import com.microblogforsim.model.User;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class JsonTool {
    private static String TAG = "fuck";
    public static String getData(String path){
        final Response[] res = {null};
        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder()
                .url(path)
                .get().build();
        final Call call = client.newCall(request);
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Response response = call.execute();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
        return res[0].toString();
    }
    public static String getHttpResult(String path){
        try {
            URL url = new URL(path);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            InputStream is = connection.getInputStream();
            connection.setRequestMethod("GET");
            connection.setConnectTimeout(5000);
            BufferedReader in = new BufferedReader(new InputStreamReader(is));
            String line;
            Log.d(TAG, "gethttpResult: " + connection.getResponseCode());
            StringBuffer sb = new StringBuffer();
            while ((line = in.readLine()) != null) {
                sb.append(line);
            }
            return sb.toString();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }
    public static JSONObject str2Json(String str){
        JSONObject jsonObject = null;
        try {
            jsonObject = new JSONObject(str);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return jsonObject;
    }
    public static ArrayList<User> getUser(JSONObject jsonObject){
        JSONArray userData = null;
        ArrayList<User> users = new ArrayList<>();
        try {
            userData = jsonObject.getJSONObject("data").getJSONArray("data");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        for (int i = 0; i < userData.length(); i++) {
            try {
                JSONObject user = userData.getJSONArray(1).getJSONObject(0);
                String name = user.getString("userName");
                String upic = user.getString("img");
                String uid = user.getString("openid");
                String phone = user.getString("phone");
                String email = user.getString("email");
                String pwd = user.getString("pwd");
                int sex = user.getInt("sex");
            //    users.add(new User(uid,name,upic,phone,email,pwd,sex));
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        return users;
    }

}
